import 'package:flutter/material.dart';

import '../dropdown_menu.dart';

// typedef DropdownMenuChange = void Function(bool isShow, int index);
typedef DropdownMenuChange = void Function(bool isShow);

class DropDownMenu extends StatefulWidget {
  // final List<DropdownMenuBuilder> menus;
  final DropdownMenuBuilder menu;
  final int animationMilliseconds;
  final Color maskColor;
  final DropdownMenuChange dropdownMenuChanging;
  final DropdownMenuChange dropdownMenuChanged;
  final DropdownMenuController controller;

  DropDownMenu(
      {Key key,
      @required this.controller,
      // @required this.menus,
      @required this.menu,
      this.animationMilliseconds = 200,
      this.maskColor = const Color.fromRGBO(0, 0, 0, 0.5),
      this.dropdownMenuChanging,
      this.dropdownMenuChanged})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _DropDownMenuState();
}

class _DropDownMenuState extends State<DropDownMenu>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  bool _isShowMask = false;
  double _maskColorOpacity;
  // int _currentMenuIndex;
  bool _isShowDropDownItemWidget = false;
  double _dropDownHeight;
  Animation<double> _animation;
  bool _isControllerDisposed = false;

  @override
  void initState() {
    super.initState();
    widget.controller.addListener(_onController);
    _animationController = AnimationController(
        vsync: this,
        duration: Duration(milliseconds: widget.animationMilliseconds));
  }

  _onController() {
    // _currentMenuIndex = widget.controller.menuIndex;
    // if (_currentMenuIndex >= widget.menus.length ||
    //     widget.menus[_currentMenuIndex] == null) {
    //   return;
    // }

    if (widget.menu == null) {
      return;
    }
    _isShowDropDownItemWidget = !_isShowDropDownItemWidget;
    if (widget.dropdownMenuChanging != null) {
      // widget.dropdownMenuChanging(_isShowDropDownItemWidget, _currentMenuIndex);
      widget.dropdownMenuChanging(_isShowDropDownItemWidget);
    }

    if (!_isShowMask) {
      _isShowMask = true;
    }

    // _dropDownHeight = widget.menus[_currentMenuIndex].dropDownHeigth;
    _dropDownHeight = widget.menu.dropDownHeigth;
    _animation?.removeListener(_animationListener);
    _animation?.removeStatusListener(_animationStatusListener);

    _animation =
        Tween(begin: 0.0, end: _dropDownHeight).animate(_animationController)
          ..addListener(_animationListener)
          ..addStatusListener(_animationStatusListener);
    if (_isControllerDisposed) {
      return;
    }
    if (widget.controller.isShow) {
      _animationController.forward();
    } else if (widget.controller.isShowHideAnimation) {
      _animationController.reverse();
    } else {
      _animationController.value = 0;
    }
  }

  void _animationListener() {
    var heightScale = _animation.value / _dropDownHeight;
    _maskColorOpacity = widget.maskColor.opacity * heightScale;
    setState(() {});
  }

  void _animationStatusListener(AnimationStatus status) {
    switch (status) {
      case AnimationStatus.dismissed:
        _isShowMask = false;
        if (widget.dropdownMenuChanged != null) {
          // widget.dropdownMenuChanged(false, _currentMenuIndex);
          widget.dropdownMenuChanged(false);
        }
        break;
      case AnimationStatus.forward:
        break;
      case AnimationStatus.reverse:
        break;
      case AnimationStatus.completed:
        if (widget.dropdownMenuChanged != null) {
          // widget.dropdownMenuChanged(true, _currentMenuIndex);
          widget.dropdownMenuChanged(true);
        }
        break;
      default:
        break;
    }
  }

  @override
  void dispose() {
    _animation?.removeListener(_animationListener);
    _animation?.removeStatusListener(_animationStatusListener);
    widget.controller?.removeListener(_onController);
    _animationController?.dispose();
    _isControllerDisposed = true;
    super.dispose();
  }

  @override
  Widget build(Object context) {
    _animationController.duration =
        Duration(milliseconds: widget.animationMilliseconds);
    return _buildDropDownWidget();
  }

  Widget _buildDropDownWidget() {
    // int menuIndex = widget.controller.menuIndex;
    // if (menuIndex >= widget.menus.length) {
    //   return Container();
    // }
    return Positioned(
        width: MediaQuery.of(context).size.width - 30,
        top: widget.controller.dropDownHeaderHeight,
        left: 15.0,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.white,
              alignment: Alignment.center,
              // padding: EdgeInsets.symmetric(horizontal: 15.0),
              // width: MediaQuery.of(context).size.width - 30,
              height: _animation == null ? 0 : _animation.value,
              // child: widget.menus[menuIndex].dropDownWidget,
              child: widget.menu.dropDownWidget,
            ),
            // _maskWidget(),
          ],
        ));
  }

  Widget _maskWidget() {
    if (_isShowMask) {
      return GestureDetector(
        onTap: () {
          widget.controller.hide();
        },
        // child: UnconstrainedBox(
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          color: widget.maskColor.withOpacity(_maskColorOpacity),
        ),
        // ),
      );
    } else {
      return Container(
        height: 0,
      );
    }
  }
}
